#include "ImplicitShapeModel.h"

#include "core/AppDirectory.h"

namespace {
const QString UserFilename = "pcl.implicit_shape_model.json";
}

ImplicitShapeModel::ImplicitShapeModel()
    : BasePclModel()
{
    setPclHelpPage("https://pcl.readthedocs.io/projects/tutorials/en/master/implicit_shape_model.html");
}

ImplicitShapeModel& ImplicitShapeModel::get()
{
    static ImplicitShapeModel instance;
    if (instance.filename().isEmpty()) {
        instance.setFilename(AppDirectory::get().getAppDocFile(UserFilename));
        instance.load();
    }
    return instance;
}

void ImplicitShapeModel::fromJson(const QJsonObject& json, QStringList& errorList)
{
    BasePclModel::fromJson(json, errorList);

    trainingFile_ = json["trainingFile"].toString(trainingFile_);
    trainingFile2_ = json["trainingFile2"].toString(trainingFile2_);
    trainingFile3_ = json["trainingFile3"].toString(trainingFile3_);
    trainingFile4_ = json["trainingFile4"].toString(trainingFile4_);
    trainingFile5_ = json["trainingFile5"].toString(trainingFile5_);
    testingFile_ = json["testingFile"].toString(testingFile_);
    normalRadiusSearch_ = json["normalRadiusSearch"].toDouble(normalRadiusSearch_);
    fpfhRadiusSearch_ = json["fpfhRadiusSearch"].toDouble(fpfhRadiusSearch_);
    samplingSize_ = json["samplingSize"].toDouble(samplingSize_);
    ismRadiusRatio_ = json["ismRadiusRatio"].toDouble(ismRadiusRatio_);
    testingCloudPointSize_ = json["testingCloudPointSize"].toInt(testingCloudPointSize_);
    testingCloudPointColor_ = QColor(json["testingCloudPointColor"].toString());
    peakPointColor_ = QColor(json["peakPointColor"].toString());
}

QJsonObject ImplicitShapeModel::toJson() const
{
    QJsonObject json = BasePclModel::toJson();

    json["trainingFile"] = trainingFile_;
    json["trainingFile2"] = trainingFile2_;
    json["trainingFile3"] = trainingFile3_;
    json["trainingFile4"] = trainingFile4_;
    json["trainingFile5"] = trainingFile5_;
    json["testingFile"] = testingFile_;
    json["normalRadiusSearch"] = normalRadiusSearch_;
    json["fpfhRadiusSearch"] = fpfhRadiusSearch_;
    json["samplingSize"] = samplingSize_;
    json["ismRadiusRatio"] = ismRadiusRatio_;
    json["testingCloudPointSize"] = testingCloudPointSize_;
    json["testingCloudPointColor"] = testingCloudPointColor_.name();
    json["peakPointColor"] = peakPointColor_.name();

    return json;
}
